package com.base;

import com.gx.util.base.BaseProcessUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;

/**
 * @author gaoxing
 * @create 2018-09-17 下午2:29
 */
public class BaseMachineUtils {
    private static final Logger log = LoggerFactory.getLogger(BaseMachineUtils.class);

    public BaseMachineUtils() {
    }

    public static String getHostName() {
        String hostName = null;

        try {
            InetAddress address = InetAddress.getLocalHost();
            hostName = address.getHostName();
        } catch (Throwable var3) {
            ;
        }

        if (StringUtils.isNotBlank(hostName) && !"localhost".equalsIgnoreCase(hostName)) {
            return hostName;
        } else {
            try {
                hostName = BaseProcessUtils.run(new String[]{"hostname"});
            } catch (Exception var2) {
                ;
            }

            return StringUtils.isNotBlank(hostName) && !"localhost".equalsIgnoreCase(hostName) ? hostName : null;
        }
    }

    public static String getIpAddress() {
        String ip = null;

        try {
            InetAddress address = InetAddress.getLocalHost();
            ip = address.getHostAddress();
        } catch (Throwable var2) {
            ;
        }

        return StringUtils.isNotBlank(ip) && !"localhost".equalsIgnoreCase(ip) && !"127.0.0.1".equals(ip) ? ip : null;
    }
}
